package pers.vic.grow.netty.chapter_1_03;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

import java.nio.charset.Charset;

/**
 * @author Vic.xu
 * @description:
 * @date 2021-09-06 16:52:44
 */
public class MyChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel channel) {

        /* 解码器*/
        //基于换行符
        channel.pipeline().addLast(new LineBasedFrameDecoder(1024));

        //基于指定字符串 【换行符 等同于LineBasedFrameDecoder】
        //channel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, false, Delimiters.lineDelimiter()))

        //基于最大程度
        //channel.pipeline().addLast(new FixedLengthFrameDecoder(4));

        //解码转string，注意编码格式
        channel.pipeline().addLast(new StringDecoder(Charset.forName("GBK")));
        //在管道中添加我们自己的接收数据实现方法
        channel.pipeline().addLast(new MyServerHandler());

    }
}
